package com.tdkj.action.backstage.website;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.tdkj.action.BaseAction;
import com.tdkj.model.website.BeanComplaint;
import com.tdkj.model.website.BeanProtocol;
import com.tdkj.model.website.BeanWebsiteNotice;
import com.tdkj.model.yellowpage.BeanEnterprice;
import com.tdkj.service.website.IBeanProtocolService;
public class ProtocolAction  extends BaseAction{
	private long id;
	private String code;
	private Timestamp createtime;
	private String title;
	private String protocoltype;
	private String content;
	private IBeanProtocolService protocolService;
	private List<BeanProtocol> protocols=new ArrayList<BeanProtocol>();
	private String[] ids;
	public String deleteAll() {
		if (null != ids) {
			for (String idtemp : ids) {
				this.getBaseService().delete(Long.parseLong(idtemp), BeanProtocol.class);
			}
		}
		return findAll();
	}
	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}
	
	public Timestamp getCreatetime() {
		return createtime;
	}
	public void setCreatetime(Timestamp createtime) {
		this.createtime = createtime;
	}
	public String getCode() { 
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getProtocoltype() {
		return protocoltype;
	}
	public void setProtocoltype(String protocoltype) {
		this.protocoltype = protocoltype;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public IBeanProtocolService getProtocolService() {
		return protocolService;
	}
	@Resource
	public void setProtocolService(IBeanProtocolService protocolService) {
		this.protocolService = protocolService;
	}
	public List<BeanProtocol> getProtocols() {
		return protocols;
	}
	public void setProtocols(List<BeanProtocol> protocols) {
		this.protocols = protocols;
	}
	//后去qq列表
	public String findAll(){
		protocols=protocolService.findAll(BeanProtocol.class);
		if(protocols.size()==0)appendObj(20);
		Criteria criteria = this.createCriteria(BeanProtocol.class);
		criteria.addOrder(Order.desc("createtime"));// 对状态进行排序
		this.queryByPage(criteria, "protocolAction!findAll.action", 10);
		return "findAll";
	}
	//获取添加界面
	public String findEditPage(){
		BeanProtocol bean=protocolService.findById(this.getId(),BeanProtocol.class);
		this.setCode(bean.getCode());
		this.setContent(bean.getContent());
		this.setTitle(bean.getTitle());
		this.setCreatetime(bean.getCreatetime());
		return "editPage";
	}
	//获取添加界面
	public String findAddPage(){
		return "addPage";
	}
	//删除协议
	public String deleteProtocol(){
		protocolService.delete(this.getId(), BeanProtocol.class);
		return findAll();
	}
	//添加协议
	public String addProtocol(){
		BeanProtocol bean=new BeanProtocol();
		bean.setCode(this.getCode());
		bean.setTitle(this.getTitle());
		bean.setProtocoltype(this.getProtocoltype());
		bean.setContent(this.getContent());
		bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
		protocolService.save(bean);
		return findAll();
	}
	//获取添加界面
	public String editProtocol(){
		BeanProtocol bean=protocolService.findById(this.getId(),BeanProtocol.class);
		bean.setId(this.getId());
		//bean.setCode(this.getCode());
		bean.setTitle(this.getTitle());
		bean.setProtocoltype(this.getProtocoltype());
		bean.setContent(this.getContent());
		bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
		protocolService.merge(bean);
		return findAll();
	}
	
	private  void appendObj(int num) {
		// TODO Auto-generated method stub
		BeanProtocol bean=null;
		for(int i=0;i<num;i++){
			bean=new BeanProtocol();
			bean.setCode("协议编号："+i);
			bean.setTitle("西南矿业第"+i+"号协议");
			bean.setContent("西南矿业第"+i+"号协议内容展示");
			bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
			protocolService.save(bean);
		}
	}
}
